package com.rd.student.ruanjian.class4.lichunquan.Grorp;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Scanner;

public class StudentMessage01 {
    static LinkedHashMap linkedHashMap = new LinkedHashMap();
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Student s = new Student(0,"李春泉","四班","软件",18, "开心");
        Student s1 = new Student(1,"张睿","四班","软件",21, "开心");
        linkedHashMap.put(s.getId(),s);
        linkedHashMap.put(s1.getId(),s1);
        while(true){
            System.out.println("学生管理系统");
            System.out.println("-----------------------");
            System.out.println("功能1 新增小组成员");
            System.out.println("功能2 移除小组成员");
            System.out.println("功能3 根据班级返回成员列表");
            System.out.println("功能4 根据专业返回成员列表");
            System.out.println("功能5 根据年龄查询成员列表 ");
            System.out.println("功能6 返回所有成员名字");
            System.out.println("------------------------");
            System.out.println("!输入功能对应号即可！");
            switch (sc.nextInt()){
                case 1:
                    addNumber();
                    break;
                case 2:
                    removeNumber();
                    break;
                case 3:
                    getNumberOfCls();
                    break;
                case 4:
                    getNumberOfMajor();
                    break;
                case 5:
                    getNumberOfAge();
                    break;
                case 6:
                    arrOfName();
                case 7:

                    break;
                default:
                    System.out.println("输入有误!请重新输入");
            }
        }
    }
//-----------------------------------------新增小组成员--------------------------------------张睿
public static void displayMenu() {
    System.out.println("========= 菜单 =========");
    System.out.println("1. 录入学生信息");
    System.out.println("2. 输出全部学生信息");
    System.out.println("3. 返回主菜单");
    System.out.println("=======================");
    System.out.println("请选择操作：");
}
    public static void addNumber(){
        Student s=new Student();
        boolean exit = false;
        Scanner scanner=new Scanner(System.in);
        while (!exit) {
            displayMenu();
            String choice = scanner.nextLine();
            switch (choice) {
                case "1":
                    addstudents();
                    break;
                case "2":
                    printStudentList();
                    break;
                case "3":
                    exit = true;
                    System.out.println("返回主菜单");
                    break;
                default:
                    System.out.println("无效的选择，请重新输入");
                    break;
            }
        }
    }
    public static void addstudents() {
        Scanner sc = new Scanner(System.in);
        int counter = linkedHashMap.size();
        int id = counter++;
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入班级");
        String cls = sc.next();
        System.out.println("请输入专业");
        String major = sc.next();
        System.out.println("请输入年龄");
        int age = sc.nextInt();
        System.out.println("请输入描述");
        String about = sc.next();
        addStudent(id, name, cls, major, age, about);

    }
    public static LinkedHashMap addStudent(int id, String name, String cls, String major, int age, String about) {
        Student student = new Student(id, name, cls, major, age, about);
        for (int i = 0; i < linkedHashMap.size(); i++) {
            Student s = (Student) linkedHashMap.get(i);
            if (student.getName().equals(s.getName())||student.getAge()>100&student.getAge()<0) {
                System.out.println("已经存在该学生，或年龄错误，请重新录入！,");
                return linkedHashMap;
            }
        }
        linkedHashMap.put(student.getId(), student);
        System.out.println("输入成功！");
        return linkedHashMap;
    }

    public static void printStudentList() {
        for (int i = 0; i < linkedHashMap.size(); i++) {
            System.out.println(linkedHashMap.get(i));
        }
    }

//-----------------------------------------新增小组成员-----------------------------------------------张睿
//-----------------------------------------移除小组成员-----------------------------------------------王星凯
    public static void removeNumber() {
        Scanner sc = new Scanner(System.in);
        boolean a = true;
        String key = null;
        HashMap hashMap =new HashMap();
        int sum  = 0;
        Student s ;
        for (int i = 0; i < linkedHashMap.size(); i++) {
            s=(Student) linkedHashMap.get(i);
            key = s.getCls();
            for (int j = 0; j < linkedHashMap.size(); j++) {
                Student s_cls =(Student) linkedHashMap.get(j);
                if(s_cls.getCls() == key) sum++;
            }
        }
        hashMap.put(key,sum);
        while (a) {
            System.out.println("1根据名字删除，2根据学号删除,3回到主页面退出此方法");
            switch (sc.nextInt()) {
                case 1:
                    System.out.println("输入要删除的");
                    for (int i = 0; i < linkedHashMap.size(); i++) {
                         s = (Student) linkedHashMap.get(i);
                        String ccc=sc.next();
                        if (s.getName().equals(ccc) && (int)hashMap.get(s.getCls())> 1) {
                            linkedHashMap.remove(i);
                            break;
                        } else if (i == linkedHashMap.size() - 1 && s.getName().equals(ccc)) {
                            System.out.println("你输入错了");
                            break;
                        }
                    }

                    break;
                case 2:
                    System.out.println("输入要删除的姓名");
                    String cc = sc.next();
                    for (int i = 0; i < linkedHashMap.size(); i++) {
                        Student d = (Student) linkedHashMap.get(i);
                        if (d.getName().equals(cc) && (int)hashMap.get(d.getCls()) > 1) {
                            linkedHashMap.remove(i);
                            break;
                        } else if (i == linkedHashMap.size() - 1 && !d.getName().equals(cc)) {
                            System.out.println("你输入错了");
                            break;
                        }
                    }
                    case 3:
                        a = false;
                        break;
                    default:
                        System.out.println("你输入错了");
                }
            }
        }
//------------------------------------------根据班级返回成员列表-----------------------------------------柏云耀
public static LinkedHashMap cls(String cls, LinkedHashMap linkedHashMap) {
    LinkedHashMap map1 = new LinkedHashMap();
    int sum = 0;
    Student student;
    for (int i = 0; i < linkedHashMap.size(); i++) {
        student = (Student) linkedHashMap.get(i);
        if (student.getCls().equals(cls)) {
            sum++;
        }
    }
    map1.put(cls,sum);
    return map1;
}
    public static void getNumberOfCls() {
        Scanner scanner = new Scanner(System.in);
        Student student;
        while (true) {
            System.out.println("根据班级查找学生");
            System.out.println("请输入学生班级：");
            String cls = scanner.next();
            System.out.println(cls(cls, linkedHashMap));
            for (int i = 0; i < linkedHashMap.size(); i++) {
                student = (Student) linkedHashMap.get(i);
                if (cls.equals(student.getCls())) {
                    System.out.println(student);
                } else {
                    System.out.println("无该班级学生");
                }
            }
            break;

        }
    }
//------------------------------------------根据班级返回成员列表-----------------------------------------柏云耀
// -----------------------------------------根据专业返回成员列表-----------------------------------------杜航
    public static  void getNumberOfMajor(){
        Scanner scanner = new Scanner(System.in);
        Student student ;
        boolean a = true;
        while (a) {
            System.out.println("---------------------");
            System.out.println("根据专业查找学生");
            System.out.println("请输入学生专业：");
            System.out.println("---------------------");
            String zhuanye = scanner.next();
            for (int i = 0; i < linkedHashMap.size(); i++) {
                student = (Student) linkedHashMap.get(i);
                if (zhuanye.equals(student.getMajor())) {
                    System.out.println(student);
                }
                else {
                    System.out.println("无该专业学生");
                }
            }
            break;
        }
        a = false;
    }

//-----------------------------------------根据专业返回成员列表-----------------------------------------杜航
//-----------------------------------------根据年龄查询成员列表-----------------------------------------高艺铭
    public static void getNumberOfAge(){
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.println("------------");
            System.out.println("功能 1：输入具体专业查看对应学生信息");
            System.out.println("功能 2：根据年龄范围返回学生信息");
            System.out.println("功能 3：计算平均年龄");
            System.out.println("功能 4：输出小于平均年龄的学生信息");
            System.out.println("功能 5：输出大于平均年龄的学生信息");
            System.out.println("功能 6：退回主菜单");
            System.out.println("-------------");
            switch (scanner.next()){
                case "1":
                    System.out.println("请输入要查询的专业如:软件");
                    String s1 = scanner.next();
                    asMajor("软件",linkedHashMap);
                    break;
                case "2":
                    System.out.println("请输入规定范围");
                    int min = Integer.parseInt(scanner.next());
                    System.out.println("到");
                    int max = Integer.parseInt(scanner.next());
                    Student s;
                    for (int i = 0;i < linkedHashMap.size();i++){
                        s = (Student) linkedHashMap.get(i);
                        if (s.getAge()>= min && s.getAge()<max){
                            System.out.println(s);
                        }
                    }
                    break;
                case "3":
                    System.out.println(eveAge(linkedHashMap));
                    break;
                case "4":
                    smallAge(linkedHashMap);
                    break;
                case "5":
                    bigAge(linkedHashMap);
                    break;
                case "6":
                    return;
                default:
                    System.out.println("指令有误");

            }

        }
    }
    public  static void asMajor(String major , LinkedHashMap linkedHashMap){
        Student s;
        for (int i = 0;i < linkedHashMap.size();i++){
            s = (Student) linkedHashMap.get(i);
            if (s.getMajor().equals(major)){
                System.out.println(s);
            }
        }
    }
    public  static int eveAge(LinkedHashMap linkedHashMap){
        int num = 0;
        Student s;
        for (int i = 0;i<linkedHashMap.size();i++){
            s =(Student) linkedHashMap.get(i);
            num+=s.getAge();
        }
        return num/ linkedHashMap.size() ;
    }

    public  static  void smallAge(LinkedHashMap linkedHashMap){
        Student s;
        for (int i = 0;i<linkedHashMap.size();i++){
            s =(Student) linkedHashMap.get(i);
            if (s.getAge() < eveAge(linkedHashMap)){
                System.out.println(s);
            }
        }

    }
    public  static void bigAge(LinkedHashMap linkedHashMap){
        Student s;
        for (int i = 0;i<linkedHashMap.size();i++){
            s =(Student) linkedHashMap.get(i);
            if (s.getAge()>=eveAge(linkedHashMap)){
                System.out.println(s);
            }
        }

    }
//--------------------------------------------------------------------------------------------------高艺铭
//    6.返回所有成员名字集合
    public static void arrOfName(){
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < linkedHashMap.size(); i++) {
            Student s =(Student) linkedHashMap.get(i);
            str.append("姓名: "+s.getName()+"\n");
        }
        System.out.println(str);
    }
}

class Student {
    private int id;
    private String name;
    private String cls;
    private String major;
    private int age;
    private String about;
    Student() {}
    public Student(int id, String name, String cls, String major, int age, String about) {
        this.id = id;
        this.name = name;
        this.cls = cls;
        this.major = major;
        this.age = age;
        this.about = about;
    }

    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getCls() {
        return cls;
    }
    public void setCls(String cls) {
        this.cls = cls;
    }
    public String getMajor() {
        return major;
    }

    public void setMajor(String major) {
        this.major = major;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAbout() {
        return about;
    }

    public void setAbout(String about) {
        this.about = about;
    }


    @Override
    public String toString() {
        return
                "Id = " + id +
                        ", 姓名 ='" + name + '\'' +
                        ", 班级 ='" + cls + '\'' +
                        ", 工作 ='" + major + '\'' +
                        ", 年龄 =" + age +
                        ", 描述 ='" + about + '\'' +
                        '}';
    }
}
